package com.java.day11;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;

public class MyCollection<E> implements Collection<E> {

    //创建数组,用来存放集合的成员
    private Object[] data = new Object[10];
    //用来表示插入元素的位置,还可以表示元素的个数
    private int count = 0;


    //返回集合元素的个数
    @Override
    public int size() {
        return count;
    }

    //返回集合是否为空
    @Override
    public boolean isEmpty() {
        if (count == 0) {
            return true;
        }
        return false;
    }


    //判断集合是否包含该元素
    @Override
    public boolean contains(Object o) {
        for (int i = 0; i < count; i++) {
            if (data[i].equals(o)) {
                return true;
            }
        }
        return false;
    }


    //判断集合是否包含该集合
    @Override
    public boolean containsAll(Collection<?> c) {
        for(Object o:c){
            if(!contains(o)){
                return false;
            }
        }
        return true;
    }

    //迭代器
    @Override
    public Iterator<E> iterator() {
        return new Iterator<E>() {

            // 迭代器对象的 下标 从0开始，到count-1
            private int index = 0;

            @Override
            public boolean hasNext() {
                return index<count;
            }

            @Override
            public E next() {
                return (E) data[index++];
            }
        };
    }

    //转数组
    @Override
    public Object[] toArray() {
        return Arrays.copyOf(data,count);
    }

    //转数组
    @Override
    public <T> T[] toArray(T[] a) {
        System.arraycopy(data,0,a,0,count);
        return a;
    }


    //向集合中添加元素
    @Override
    public boolean add(E e) {
        data[count++] = e;
        //扩容
        if (count == data.length) {
            data = Arrays.copyOf(data, data.length * 2);
        }

        return true;
    }


    //向集合中添加集合
    @Override
    public boolean addAll(Collection<? extends E> c) {
        for (E e : c) {
            add(e);
        }
        return true;
    }


    //删除集合中的元素
    @Override
    public boolean remove(Object o) {
        int index = getElementIndex(o);
        if (index!=-1){
            System.arraycopy(data,index+1,data,index,count-(index+1));
            data[count-1]=null;
            count--;
            return true;
        }
        return false;
    }


    //获取元素下标
    private int getElementIndex(Object o) {
        for (int i = 0; i < data.length; i++) {
            if(data[i].equals(o)){
                return i;
            }
        }
        return -1;
    }

    //在集合中删除集合
    @Override
    public boolean removeAll(Collection<?> c) {
        boolean flag = false;
        for(Object o:c){
            if (remove(o)){
                return true;
            }
        }
        return false;
    }

    //
    @Override
    public boolean retainAll(Collection<?> c) {
        //将要删除的元素放入集合中
        ArrayList<Object> objects = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            if(!c.contains(data[i])){
                objects.add(c);
            }
        }
        return removeAll(objects);
    }


    //清除集合元素
    @Override
    public void clear() {
        data = new Object[10];
        count = 0;
    }

    @Override
    public String toString() {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("[");
        for (int i = 0; i < count; i++) {
            stringBuffer.append(data[i]+"");
        }
        stringBuffer.append("]");
        return stringBuffer.toString();

    }
}
